<%--
  Created by IntelliJ IDEA.
  User: 22328
  Date: 2021/2/5
  Time: 14:14
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>焕耀商城－登录</title>
    <link rel="stylesheet" type="text/css" href="./css/reset.css">
    <link rel="stylesheet" type="text/css" href="./css/main.css">
    <script type="text/javascript" src="./js/jquery-3.1.0.min.js"></script>
    <%--<script src="./js/login.js"></script>--%>
</head>
<body>

<div class="login_top clearfix">
    <a href="./main" class="login_logo"><img src="images/logo.png"></a>
    <span>管理员账号：admin 密码：root</span>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span>商家账号：小米 密码：xiaomi</span>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span>普通账号：test 密码：testtest</span>
</div>

<div class="login_form_bg">
    <div class="login_form_wrap clearfix">
        <div class="login_banner fl"></div>
        <div class="slogan fl">旗舰入驻 · 正品保证</div>
        <div class="login_form fr">
            <div class="login_title clearfix">
                <h1>用户登录</h1>
                <a href="./register">立即注册</a>
            </div>
            <div class="form_input">
                <form onsubmit="return false" action="#" method="post" id="from_login">
                    <input type="text" name="username" id="username" class="name_input" placeholder="请输入用户" value="">
                    <div class="user_error">输入错误</div>
                    <input type="password" name="password" id="pwd" class="pass_input" placeholder="请输入密码"
                           value="">
                    <div class="pwd_error" style="display: none;">输入错误</div>
                    <img src="./getCode" id="get_code" alt="captcha" class="captcha">
                    <input id="id_captcha_0" name="captcha_0" type="hidden" value="">
                    <input autocapitalize="off" autocomplete="off" autocorrect="off" spellcheck="false"
                           id="id_captcha_1" name="captcha_1" type="text" placeholder="请输入验证码">
                    <div class="yanzheng_error">请输入验证码</div>
                    <div class="more_input clearfix">
                        <input type="checkbox" name="" id="" checked="checked">
                        <label>记住用户名</label>
                        <%--<a id="update_code"><i class="layui-icon layui-icon-refresh"></i></a>--%>
                        <a id="update_code" href="#">验证码刷新</a>
                    </div>
                    <input type="button" name="submit_btn" value="登录" class="input_submit">
                </form>
            </div>
        </div>
    </div>
</div>

<%@include file="bottom.jsp" %>
</body>
<script>
    $(function () {
        $("#update_code").click(function () {
            var num = Math.random();
            $("#get_code").attr("src","./getCode?num="+num);
        });

        $("[name=submit_btn]").click(function () {
            //alert("hello");
            var username = $("#username").val();
            var password = $("#pwd").val();
            var code = $("#id_captcha_1").val();
            $.ajax({
                url: "./checkLogin",
                type: "post",
                data: {username: username, password: password,validateCode:code},
                success: function (res) {
                    if (res != null && res !== '') {
                        if (res.role.roleName === '管理员' || res.role.roleName === '商家') {
                            //如果该用户是管理员那么就跳转到后台页面
                            if (res.status === 1) {
                                window.location.href = "./adminMenu?username=" + username;
                            }
                            else {
                                alert("用户已经被禁用，请联系客服");
                            }
                        }
                        if (res.role.roleName === '普通用户') {
                            //如果该用户是普通用户就跳转到前台主页面
                            window.location.href = "./main?username=" + username;
                        }
                    } else {
                        alert("用户名或密码或验证码错误");
                    }
                },
                error: function () {
                    alert("404.html页面");
                }
            });
        });
    });
</script>
</html>
